*SETUP

log using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\ISQ log.smcl", replace

clear all
set more off
graph drop _all
macro drop _all

use "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\ISQ.dta"

stset age, id(id) failure(PB)

*********

*summary stats

estpost sum PB pooling delegation avgdemocracy demdensity EUengagement regemulation globemulation post90 gnic democratizingHG democraticHG age
#delimit ;
esttab . using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb sum stats.rtf", replace
	unstack cells("mean sd min max") nomtitle nonumber onecell nogaps label title("")
; #delimit cr

*correlation matrix

estpost correlate PB pooling delegation avgdemocracy demdensity EUengagement regemulation globemulation post90 gnic democratizingHG democraticHG age, matrix
#delimit ;
esttab . using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb corr.rtf", replace
	unstack b(3) p(3) noobs nostar nonumber onecell nogaps label title("")
; #delimit cr

replace delegation=delegation*100
replace regemulation=regemulation*100
replace globemulation=globemulation*100

*main table
	
stcox pooling delegation globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo fo1

stcox avgdemocracy globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo fo2

stcox demdensity globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo fo3

stcox EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo fo4

stcox delegation avgdemocracy EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo fo5

*table  	
*ORDER 
#delimit ;
esttab fo1 fo2 fo3 fo4 fo5
	using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb cox main.rtf"
	,star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) label nomtitle onecell nogaps title("") replace
	order(pooling delegation avgdemocracy demdensity EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG)
	stats(N chi2 p, fmt(3) label("N" "Wald chi2" "Prob > chi2")) eform
; #delimit cr

*********

*disaggregated EU engagement

stcox EUfund regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo ffa1

stcox EUcont regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo ffa2

stcox EUpbcont regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust)
eststo ffa3

*table  	
*ORDER 
#delimit ;
esttab ffa1 ffa2 ffa3
	using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb cox eu.rtf"
	,star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) label nomtitle onecell nogaps title("") replace
	order(EUfund EUcont EUpbcont regemulation globemulation post90 gdp democratizingHG democraticHG)
	stats(N chi2 p, fmt(3) label("N" "Wald chi2" "Prob > chi2")) eform
; #delimit cr

*strata age

stcox delegation pooling globemulation post90 gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffc1

stcox avgdemocracy globemulation post90 gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffc2

stcox demdensity globemulation post90 gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffc3

stcox EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffc4

stcox delegation avgdemocracy EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffc5

*table  	
*ORDER 
#delimit ;
esttab ffc1 ffc2 ffc3 ffc4 ffc5
	using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb cox age.rtf"
	,star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) label nomtitle onecell nogaps title("") replace
	order(pooling delegation avgdemocracy demdensity EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG)
	stats(N chi2 p, fmt(3) label("N" "Wald chi2" "Prob > chi2")) eform
; #delimit cr

*strata age year

stcox delegation pooling year gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffcx1

stcox avgdemocracy year gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffcx2

stcox demdensity year gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffcx3

stcox EUengagement regemulation year gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffcx4

stcox delegation avgdemocracy EUengagement regemulation year gdp democratizingHG democraticHG, vce(robust) strata(age)
eststo ffcx5

*table  	
*ORDER 
#delimit ;
esttab ffcx1 ffcx2 ffcx3 ffcx4 ffcx5
	using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb cox age year.rtf"
	,star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) label nomtitle onecell nogaps title("") replace
	order(pooling delegation avgdemocracy demdensity EUengagement regemulation year gdp democratizingHG democraticHG)
	stats(N chi2 p, fmt(3) label("N" "Wald chi2" "Prob > chi2")) eform
; #delimit cr

*weibull

streg pooling delegation globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(w)
eststo ffd1

streg avgdemocracy globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(w)
eststo ffd2

streg demdensity globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(w)
eststo ffd3

streg EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(w)
eststo ffd4

streg delegation avgdemocracy EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(w)
eststo ffd5

*table  	
*ORDER 
#delimit ;
esttab ffd1 ffd2 ffd3 ffd4 ffd5
	using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb cox weibull.rtf"
	,star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) label nomtitle onecell nogaps title("") replace
	order(pooling delegation avgdemocracy demdensity EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG)
	stats(N chi2 p, fmt(3) label("N" "Wald chi2" "Prob > chi2")) eform
; #delimit cr

*gompertz

streg pooling delegation globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(gompertz)
eststo ffe1

streg avgdemocracy globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(gompertz)
eststo ffe2

streg demdensity globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(gompertz)
eststo ffe3

streg EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(gompertz)
eststo ffe4

streg delegation avgdemocracy EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG, vce(robust) d(gompertz)
eststo ffe5

*table  	
*ORDER 
#delimit ;
esttab ffe1 ffe2 ffe3 ffe4 ffe5
	using "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\pb cox gompertz.rtf"
	,star(* 0.1 ** 0.05 *** 0.01) b(3) se(3) label nomtitle onecell nogaps title("") replace
	order(pooling delegation avgdemocracy demdensity EUengagement regemulation globemulation post90 gdp democratizingHG democraticHG)
	stats(N chi2 p, fmt(3) label("N" "Wald chi2" "Prob > chi2")) eform
; #delimit cr
	
*********

*figures

*count

egen ttl=count(id), by(year)
xtset id year, yearly
#delimit ;
graph twoway
	tsline PBcount, lw(medthick) lc("120 120 120")
	|| tsline ttl, lw(medthick) lc("0 0 0")
	||, tlabel(1950(10)2010) ylabel(0(5)35, grid glw(vthin) glc("200 200 200")) xtitle("Year", margin(2 2 2 2)) ytitle("Number of ROs", margin(2 2 2 2))
	legend(size(medlarge) order(1 "with parliament" 2 "total") keygap(0.5) bm(zero)) 
	graphregion(margin(l=2 r=8 b=2 t=8)) graphregion(color(white) lwidth(vthick)) plotregion(icolor(white) m(zero)) name(ttl, replace) xsize(6) ysize(4)
; #delimit cr	
graph export "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\ISQ fig1.png", replace width(600) height(400)
xtset, clear
drop ttl

*survival functions

replace delegation=delegation/100
sum delegation EUengagement regemulation
egen delx=cut(delegation), icodes at(-0.1,0.21,1.1)
egen pbeux=cut(EUengagement), icodes at(-0.1,1,6.1)
egen pbrx=cut(regemulation), icodes at(-0.1,25.1,80.1)

#delimit ;
sts graph, failure by(delx) tmax(60) xtitle("RO age", c(black) size(medlarge) margin(0 0 2 2)) title("Delegation", c(black) margin(0 0 2 0)) yscale(r(., 1)) 
tlabel(0(5)60) ylabel(0(0.1)1, grid glw(vthin) glc("200 200 200") format(%4.2g)) 
legend(size(medlarge) order(1 "low" 2 "high") keygap(0.5) bm(zero)) 
plot1(lw(medthick) lc("0 0 0")) plot2(lw(medthick) lc("120 120 120"))
graphregion(margin(l=2 r=8 b=2 t=2)) graphregion(color(white) lwidth(vthick)) plotregion(icolor(white) m(zero)) name(delx, replace)
; #delimit cr
#delimit ;
sts graph, failure by(pbeux) tmax(60) xtitle("RO age", c(black) size(medlarge) margin(0 0 2 2)) title("EU engagement", c(black) margin(0 0 2 0)) yscale(r(., 1))  
tlabel(0(5)60) ylabel(0(0.1)1, grid glw(vthin) glc("200 200 200") format(%4.2g)) 
legend(size(medlarge) order(1 "low" 2 "high") keygap(0.5) bm(zero)) 
plot1(lw(medthick) lc("0 0 0")) plot2(lw(medthick) lc("120 120 120"))
graphregion(margin(l=2 r=8 b=2 t=2)) graphregion(color(white) lwidth(vthick)) plotregion(icolor(white) m(zero)) name(pbeux, replace)
; #delimit cr
#delimit ;
sts graph, failure by(pbrx) tmax(60) xtitle("RO age", c(black) size(medlarge) margin(0 0 2 2)) title("Regional emulation", c(black) margin(0 0 2 0)) yscale(r(., 1)) 
tlabel(0(5)60) ylabel(0(0.1)1, grid glw(vthin) glc("200 200 200") format(%4.2g)) 
legend(size(medlarge) order(1 "low" 2 "high") keygap(0.5) bm(zero)) 
plot1(lw(medthick) lc("0 0 0")) plot2(lw(medthick) lc("120 120 120"))
graphregion(margin(l=2 r=8 b=2 t=2)) graphregion(color(white) lwidth(vthick)) plotregion(icolor(white) m(zero)) name(pbrx, replace)
; #delimit cr
graph combine delx pbeux pbrx, xsize(12) ysize(4) graphregion(margin(l=2 r=2 b=4 t=4)) graphregion(color(white) lwidth(vthick)) iscale(0.9) rows(1)
graph export "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\ISQ fig2.png", replace width(1200) height(400)

drop delx pbeux pbrx

*********

*CLEANUP
macro drop _all

#delimit ;
translate "D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\ISQ log.smcl"
"D:\Dropbox\0 project\crepuscular 2 B\00 data raw\parliaments\survival\v5\isq\ISQ log.pdf"
; #delimit cr
log close 
